package com.chrray.position.service.web.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.chrray.position.domain.FdJobPostings;
import com.chrray.position.domain.FdUser;
import com.chrray.position.enums.YesNoEnum;
import com.chrray.position.enums.app.FdJobCheckStateEnum;
import com.chrray.position.enums.app.FdJobStateEnum;
import com.chrray.position.mapper.position.FdJobPostingsMapper;
import com.chrray.position.mapper.position.FdUserMapper;
import com.chrray.position.pojo.request.web.IndexDto;
import com.chrray.position.pojo.vo.web.*;
import com.chrray.position.service.web.WFdIndexService;
import com.chrray.position.util.result.ResponseResult;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;

import javax.annotation.Resource;

import java.time.DayOfWeek;
import java.time.LocalDate;
import java.time.ZoneOffset;
import java.time.temporal.TemporalAdjusters;
import java.util.*;
import java.util.stream.Collectors;

import static com.chrray.position.util.result.ResponseResults.success;

/**
 * @author LM
 * @date 2024/9/3 10:50
 */
@Service
public class WFdIndexServiceImpl implements WFdIndexService {
    @Resource
    private FdJobPostingsMapper fdJobPostingsMapper;
    @Resource
    private FdUserMapper fdUserMapper;


    @Override
    public ResponseResult<PositionStaticsVo> positionStatics() {
        PositionStaticsVo vo = new PositionStaticsVo();
        LambdaQueryWrapper<FdJobPostings> queryWrapper = new LambdaQueryWrapper<>();
        Integer allNum = fdJobPostingsMapper.selectCount(queryWrapper);
        vo.setAllNum(allNum);
        LambdaQueryWrapper<FdJobPostings> queryWrapper1 = new LambdaQueryWrapper<>();
        queryWrapper1.eq(FdJobPostings::getStatus, FdJobStateEnum.OFF_SHELF.getValue());
        Integer outLineNum = fdJobPostingsMapper.selectCount(queryWrapper1);
        vo.setOutLineNum(outLineNum);
        LambdaQueryWrapper<FdJobPostings> queryWrapper2 = new LambdaQueryWrapper<>();
        queryWrapper2.eq(FdJobPostings::getCheckStatus, FdJobCheckStateEnum.PENDING);
        Integer waitCheckNum = fdJobPostingsMapper.selectCount(queryWrapper2);
        vo.setWaitCheckNum(waitCheckNum);
        LambdaQueryWrapper<FdJobPostings> queryWrapper3 = new LambdaQueryWrapper<>();
        queryWrapper3.eq(FdJobPostings::getStatus, FdJobStateEnum.MATCHING.getValue());
        Integer recruitmentNum = fdJobPostingsMapper.selectCount(queryWrapper3);
        vo.setRecruitmentNum(recruitmentNum);

        return success(vo);
    }

    @Override
    public ResponseResult<UserStaticsVo> userStatics() {
        UserStaticsVo vo = new UserStaticsVo();
        LambdaQueryWrapper<FdUser> queryWrapper = new LambdaQueryWrapper<>();
        Integer allNum = fdUserMapper.selectCount(queryWrapper);
        vo.setAllNum(allNum);
        queryWrapper.eq(FdUser::getRealNamed, YesNoEnum.YES.getValue());
        Integer certNum = fdUserMapper.selectCount(queryWrapper);
        vo.setCertNum(certNum);
        Calendar instance = Calendar.getInstance();
        instance.set(Calendar.HOUR_OF_DAY, 0); // 设置小时为12点
        instance.set(Calendar.MINUTE, 0); // 设置分钟为0
        instance.set(Calendar.SECOND, 0);
        LambdaQueryWrapper<FdUser> queryWrapper1 = new LambdaQueryWrapper<>();
        queryWrapper1.ge(FdUser::getCreateTime, instance.getTime());
        Integer todayAddNum = fdUserMapper.selectCount(queryWrapper1);
        vo.setTodayAddNum(todayAddNum);
        LambdaQueryWrapper<FdUser> queryWrapper2 = new LambdaQueryWrapper<>();
        instance.set(Calendar.DAY_OF_MONTH, 1); // 设置日期为1号
        queryWrapper2.ge(FdUser::getCreateTime, instance.getTime());
        Integer mouthAddNum = fdUserMapper.selectCount(queryWrapper2);
        vo.setMouthAddNum(mouthAddNum);

        return success(vo);
    }

    @Override
    public ResponseResult<PositionStaticsLeftVo> positionStaticsLeft() {
        PositionStaticsLeftVo vo = new PositionStaticsLeftVo();
        LambdaQueryWrapper<FdJobPostings> queryWrapper = new LambdaQueryWrapper<>();
        LocalDate now = LocalDate.now();
        now = now.with(TemporalAdjusters.previousOrSame(DayOfWeek.MONDAY));
        queryWrapper.ge(FdJobPostings::getStaTime, now);
        Integer weekNum = fdJobPostingsMapper.selectCount(queryWrapper);
        vo.setWeekNum(weekNum);
        LambdaQueryWrapper<FdJobPostings> queryWrapper1 = new LambdaQueryWrapper<>();
        LocalDate now1 = LocalDate.now();
        now1 = now1.withDayOfMonth(1);
        queryWrapper1.ge(FdJobPostings::getStaTime, now1);
        Integer mouthNum = fdJobPostingsMapper.selectCount(queryWrapper1);
        vo.setMouthNum(mouthNum);
        return success(vo);

    }

    @Override
    public ResponseResult<List<PositionStaticsRightVo>> positionStaticsRight(IndexDto indexDto) {
        List<PositionStaticsRightVo> list = fdJobPostingsMapper.positionStaticsRight(indexDto.getStartTime(), indexDto.getEndTime());

        Map<Date, PositionStaticsRightVo> mapList = list.stream()
                .collect(Collectors.toMap(PositionStaticsRightVo::getStaDate, str -> str));
        List<PositionStaticsRightVo> result=new ArrayList<>();
        LocalDate startLocalDate=indexDto.getStartTime();
        LocalDate endLocalDate=indexDto.getEndTime();
        do {
            PositionStaticsRightVo vo = new PositionStaticsRightVo();
            Date from = Date.from(startLocalDate.atStartOfDay(ZoneOffset.ofHours(8)).toInstant());
            vo.setStaDate(from);
            PositionStaticsRightVo mapVo = mapList.get(from);
            vo.setNum(ObjectUtils.isEmpty(mapVo)?0:mapVo.getNum());
            result.add(vo);
            startLocalDate=  startLocalDate.plusDays(1);
        }while (!startLocalDate.isAfter(endLocalDate));
        return success(result);
    }

    @Override
    public ResponseResult<UserStaticsLeftVo> userStaticsLeft() {
        UserStaticsLeftVo vo = new UserStaticsLeftVo();
        LambdaQueryWrapper<FdUser> queryWrapper = new LambdaQueryWrapper<>();
        LocalDate now = LocalDate.now();
        now = now.with(TemporalAdjusters.previousOrSame(DayOfWeek.MONDAY));
        queryWrapper.ge(FdUser::getStaTime, now);
        Integer weekNum = fdUserMapper.selectCount(queryWrapper);
        vo.setWeekNum(weekNum);
        LambdaQueryWrapper<FdUser> queryWrapper1 = new LambdaQueryWrapper<>();
        LocalDate now1 = LocalDate.now();
        now1 = now1.withDayOfMonth(1);
        queryWrapper1.ge(FdUser::getStaTime, now1);
        Integer mouthNum = fdUserMapper.selectCount(queryWrapper1);
        vo.setMouthNum(mouthNum);
        return success(vo);
    }

    @Override
    public ResponseResult<List<UserStaticsRightVo>> userStaticsRight(IndexDto indexDto) {
        List<UserStaticsRightVo> list = fdUserMapper.positionStaticsRight(indexDto.getStartTime(), indexDto.getEndTime());

        Map<Date, UserStaticsRightVo> mapList = list.stream()
                .collect(Collectors.toMap(UserStaticsRightVo::getStaDate, str -> str));
        List<UserStaticsRightVo> result=new ArrayList<>();
        LocalDate startLocalDate=indexDto.getStartTime();
        LocalDate endLocalDate=indexDto.getEndTime();
        do {
            UserStaticsRightVo vo = new UserStaticsRightVo();
            Date from = Date.from(startLocalDate.atStartOfDay(ZoneOffset.ofHours(8)).toInstant());
            vo.setStaDate(from);
            UserStaticsRightVo mapVo = mapList.get(from);
            vo.setNum(ObjectUtils.isEmpty(mapVo)?0:mapVo.getNum());
            result.add(vo);
            startLocalDate=  startLocalDate.plusDays(1);
        }while (!startLocalDate.isAfter(endLocalDate));
        return success(result);
    }
}
